package cn.wit.test;

import java.util.Arrays;

/**
 * 优化前面的矩阵每列元素和
 * 
 * @author 16604
 *
 */
public class DemoOptimisticMatrixElementSum {
	public static void main(String[] args) {
		double[][] a = { { 1.5, 2, 3, 4 }, { 2.3, 5, 8, 9 }, { 3, 5.4, 7, 10 } };
		System.out.println(Arrays.toString(sumColumns(a)));
	}

	public static double[] sumColumns(double[][] matrix) {

		// 定义一个double数组，用来存放求出列元素的和
		double[] num = new double[matrix[0].length];
		int colnum = 0;
		while (colnum <= matrix[0].length-1) {
			double count =0;
			for (int i = 0; i <matrix.length ; i++) {
				count = count + matrix[i][colnum];
			}
			num[colnum++] = count;
		}
		return num;
	}

}
